package pack2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main2FinalData {

	public static void main (String[] args) throws Exception{

		String path = "FinalFile.txt";
		List<FO>f = new ArrayList<FO>();
		//Map<String, List<Integer>>map = new TreeMap<String, List<Integer>>();
		Map<String, Map<Integer, Integer>>map2 = new HashMap<String, Map<Integer, Integer>>();

		//1
		loadfinalData(path, f);
		List<Integer>in = new ArrayList<Integer>();

		for(FO fo: f) {
			Map<Integer, Integer> m = map2.get(fo.h1);
			if(m == null) {
				m = new TreeMap<Integer,Integer>();
				map2.put(fo.h1, m);
			}
			Integer fx = m.get(fo.freq);
			if(fx == null) m.put(fo.freq, 1);
			else m.put(fo.freq, fx+1);
		}

		for(Map<Integer, Integer> m : map2.values()) {
			int max = 0;
			for(int x: m.keySet())
				max = Math.max(max, x);
			System.out.println("MAX = "+max);
		}



		for(String s: map2.keySet()){
			System.out.print(s);
			for(int i=1;i<1000;i++) {
				int somma = 0; 
				for(int j: map2.get(s).keySet())
					if(j>=i) somma += map2.get(s).get(j);

				System.out.print(","+i+"="+somma);
				if(somma == 1) break;
			}
			System.out.println();
		}
	}

	  public static void print(String file, Map<String, Map<Integer,Integer>>map)throws Exception{
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(file)));
			
			for (String s: map.keySet()) {
			
				out.print(s+" ");
				for (int i : map.get(s).keySet()) {
					out.print(i+":");
					out.print(map.get(s).get(i)+" ");
				}
				out.println();
			}
			out.close();
		}
	
	public static void loadfinalData(String file, List<FO>list)throws Exception{
		
		BufferedReader br = new BufferedReader(new FileReader(new File(file)));
		
		String line;
		
		while((line = br.readLine())!=null){

			String [] riga = line.split(", ");
			
			String h1 = riga[0];
			String h2 = riga[1];
			int freq = Integer.parseInt(riga[2]);
				list.add(new FO(h1, h2, freq));
		}
		br.close();
	}
}
